#include <iostream>
#include <cmath>
using namespace std;
int number(int num, int digit){
assert(digit>0);
int upper=pow(10, digit), down=pow(10, digit-1);
return (num%upper)/down;
}
void CountingSort(int* A, const int k, int size, int digit){
int* C=new int[k+1];
int* B=new int[size];
for(int j=0; j<size; ++j){
C[number(A[j], digit)]++;
}
for(int i=1; i<k+1; ++i){
C[i]+=C[i-1];
}
for(int j=size-1; j>=0; --j){
B[C[number(A[j], digit)]-1]=A[j];
C[number(A[j], digit)]--;
}
for(int i=0; i<size; ++i){
A[i]=B[i];
}
delete[] B;
delete[] C;
}
void RadixSort(int*A, int d, int size){
int* B=new int[size];
for(int i=1; i<=d; ++i){
CountingSort(A, 9, size, i);
}
delete[] B;
}
int main(void){
int arr[]={329, 457, 657, 839, 436, 720, 355};
int num_size=3;
int size=sizeof(arr)/sizeof(int);
RadixSort(arr, num_size, size);
for(int i=0; i<size-1; ++i){
cout<<arr[i]<<", ";
}
cout<<arr[size-1]<<endl;
return 0;
}